package com.wpen.unit.utils;

import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.util.List;
import java.util.Random;

/**
 * string类型封装处理类
 * 
 * @author Admin
 *
 */
public class StringUtil {

    public static boolean isBlankChar(char c) {
        return isBlankChar((int) c);
    }

    public static boolean isBlankChar(int c) {
        return (Character.isWhitespace(c)) || (Character.isSpaceChar(c));
    }

    public static boolean isBlank(CharSequence str) {
        int length;
        if ((str == null) || ((length = str.length()) == 0))
            return true;
        for (int i = 0; i < length; i++) {
            if (false == isBlankChar(str.charAt(i))) {
                return false;
            }
        }
        return true;
    }

    public static boolean isNotBlank(CharSequence string) {
        return false == isBlank(string);
    }

    public static boolean isEmpty(String chkStr) {
        if (chkStr == null)
            return true;
        return "".equals(chkStr.trim());
    }

    public static boolean isNotEmpty(String str) {
        return false == isEmpty(str);
    }

    public static boolean isEmpty(CharSequence str) {
        return (str == null) || (str.length() == 0);
    }

    public static boolean isNotEmpty(CharSequence str) {
        return false == isEmpty(str);
    }

    public static byte[] bytes(CharSequence str) {
        return bytes(str, Charset.defaultCharset());
    }

    public static byte[] bytes(CharSequence str, String charset) {
        return bytes(str, isBlank(charset) ? Charset.defaultCharset() : Charset.forName(charset));
    }

    public static byte[] bytes(CharSequence str, Charset charset) {
        if (str == null) {
            return null;
        }
        if (null == charset) {
            return str.toString().getBytes();
        }
        return str.toString().getBytes(charset);
    }


    public static String str(byte[] bytes, String charset) {
        return str(bytes, isBlank(charset) ? Charset.defaultCharset() : Charset.forName(charset));
    }

    public static String str(byte[] data, Charset charset) {
        if (data == null) {
            return null;
        }
        if (null == charset) {
            return new String(data);
        }
        return new String(data, charset);
    }

    public static String str(Byte[] bytes, String charset) {
        return str(bytes, isBlank(charset) ? Charset.defaultCharset() : Charset.forName(charset));
    }

    public static String str(Byte[] data, Charset charset) {
        if (data == null) {
            return null;
        }
        byte[] bytes = new byte[data.length];
        for (int i = 0; i < data.length; i++) {
            Byte dataByte = data[i];
            bytes[i] = (null == dataByte ? -1 : dataByte.byteValue());
        }

        return str(bytes, charset);
    }

    public static String str(ByteBuffer data, String charset) {
        if (data == null) {
            return null;
        }
        return str(data, Charset.forName(charset));
    }

    public static String str(ByteBuffer data, Charset charset) {
        if (null == charset) {
            charset = Charset.defaultCharset();
        }
        return charset.decode(data).toString();
    }

    public static boolean afterTrimIsBlock(String string) {
        if (isBlank(string))
            return true;
        string = string.trim();
        if (isBlank(string))
            return true;
        return false;
    }

    public static boolean afterTrimIsNotBlock(String string) {
        return false == afterTrimIsBlock(string);
    }

    public static String upperFirst(String str) {
        if (null == str) {
            return null;
        }
        if (str.length() > 0) {
            char firstChar = str.charAt(0);
            if (Character.isLowerCase(firstChar)) {
                return new StringBuilder().append(Character.toUpperCase(firstChar)).append(str.substring(1)).toString();
            }
        }
        return str.toString();
    }

    public static String lowerFirst(String str) {
        if (null == str) {
            return null;
        }
        if (str.length() > 0) {
            char firstChar = str.charAt(0);
            if (Character.isUpperCase(firstChar)) {
                return new StringBuilder().append(Character.toLowerCase(firstChar)).append(str.substring(1)).toString();
            }
        }
        return str.toString();
    }

    /**
     * unicode 转换成 utf-8
     * 
     * @author fanhui 2007-3-15
     * @param theString
     * @return
     */
    public static String unicode2Utf8(String theString) {
        char aChar;
        int len = theString.length();
        StringBuffer outBuffer = new StringBuffer(len);
        for (int x = 0; x < len;) {
            aChar = theString.charAt(x++);
            if (aChar == '\\') {
                aChar = theString.charAt(x++);
                if (aChar == 'u') {
                    // Read the xxxx
                    int value = 0;
                    for (int i = 0; i < 4; i++) {
                        aChar = theString.charAt(x++);
                        switch (aChar) {
                        case '0':
                        case '1':
                        case '2':
                        case '3':
                        case '4':
                        case '5':
                        case '6':
                        case '7':
                        case '8':
                        case '9':
                            value = (value << 4) + aChar - '0';
                            break;
                        case 'a':
                        case 'b':
                        case 'c':
                        case 'd':
                        case 'e':
                        case 'f':
                            value = (value << 4) + 10 + aChar - 'a';
                            break;
                        case 'A':
                        case 'B':
                        case 'C':
                        case 'D':
                        case 'E':
                        case 'F':
                            value = (value << 4) + 10 + aChar - 'A';
                            break;
                        default:
                            throw new IllegalArgumentException("Malformed   \\uxxxx   encoding.");
                        }
                    }
                    outBuffer.append((char) value);
                } else {
                    if (aChar == 't')
                        aChar = '\t';
                    else if (aChar == 'r')
                        aChar = '\r';
                    else if (aChar == 'n')
                        aChar = '\n';
                    else if (aChar == 'f')
                        aChar = '\f';
                    outBuffer.append(aChar);
                }
            } else
                outBuffer.append(aChar);
        }
        return outBuffer.toString();
    }

    /**
     * 获取数字型随机数字
     * 
     * @param numLen 随机码位数
     * @return
     * @throws Exception
     */
    public static String getRandomNum(int numLen) {
        if (numLen <= 0 || numLen >= 100)
            numLen = 6;
        String rs = "";
        for (int i = 0; i < numLen; i++) {
            Random random = new Random();
            int randomInt = random.nextInt() % 10;
            rs += Math.abs(randomInt);
        }
        return rs;
    }

    public static String list2String(List<?> list) {
        if (list == null || list.size() == 0)
            return "";
        StringBuffer stringBuffer = new StringBuffer();
        for (Object object : list) {
            if (object != null) {
                stringBuffer.append(object.toString() + ",");
            }
        }
        String rs = stringBuffer.toString();
        return rs.substring(0, rs.length() - 1);
    }

}
